package com.longai.service;

import javax.imageio.ImageIO;

import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.io.InputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import util.Page;

import com.longai.dao.*;
import com.longai.model.*;

import functions.GetProperties;

@Service
public class PictureService {
	@Autowired
	private PictureDAO pictureDAO;

	public Picture findById(int id) {
		return pictureDAO.findById(id);
	}

	public void save(Picture picture) {
		pictureDAO.save(picture);
	}

	public void delete(Picture picture) {
		pictureDAO.delete(picture);
	}

	public List<Picture> findAll() {
		return pictureDAO.findAll();
	}

	public List<Picture> findPicByUserId(String str) {
		return pictureDAO.findByUserId(str);
	}

	public static BufferedImage resize(BufferedImage source, int targetW,
			int targetH) {
		// targetW，targetH分别表示目标长和宽
		int type = source.getType();
		BufferedImage target = null;
		double sx = (double) targetW / source.getWidth();
		double sy = (double) targetH / source.getHeight();
		// 这里想实现在targetW，targetH范围内实现等比缩放。如果不需要等比缩放
		// 则将下面的if else语句注释即可
		if (sx > sy) {
			sx = sy;
			targetW = (int) (sx * source.getWidth());
		} else {
			sy = sx;
			targetH = (int) (sy * source.getHeight());
		}
		if (type == BufferedImage.TYPE_CUSTOM) { // handmade
			ColorModel cm = source.getColorModel();
			WritableRaster raster = cm.createCompatibleWritableRaster(targetW,
					targetH);
			boolean alphaPremultiplied = cm.isAlphaPremultiplied();
			target = new BufferedImage(cm, raster, alphaPremultiplied, null);
		} else
			target = new BufferedImage(targetW, targetH, type);
		Graphics2D g = target.createGraphics();
		// smoother than exlax:
		g.setRenderingHint(RenderingHints.KEY_RENDERING,
				RenderingHints.VALUE_RENDER_QUALITY);
		g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
		g.dispose();
		return target;
	}

	public static void saveImageAsJpg(BufferedImage srcImage, String imgType,
			String saveToFileStr, int width, int hight) throws Exception {

		File saveFile = new File(saveToFileStr);

		if (width > 0 || hight > 0) {
			srcImage = resize(srcImage, width, hight);
		}
		ImageIO.write(srcImage, imgType, saveFile);
	}

	public static void deleteImage(String pack, String name, String webRoot) {
		String oriPath = GetProperties.getValue("config", pack);
		String nailPath = GetProperties.getValue("config", "nail");
		File file = new File(webRoot + oriPath + name);
		System.out.println(webRoot + oriPath + name);
		if (file.isFile()) {
			file.delete();
		}
		file = new File(webRoot + nailPath + name);
		if (file.isFile()) {
			file.delete();
		}
	}

	public Page findByUserId(String str, String currentPage, String amount) {
		return pictureDAO.findByUserId(str, currentPage, amount);
	}
}
